//
//  QJMineEditAddressVC.m
//  QuanJieShopProject
//
//  Created by apple on 2018/2/24.
//  Copyright © 2018年 apple. All rights reserved.
//

#define Key_DistrictSelectProvince          @"DistrictSelectProvince"
#define Key_DistrictSelectProvinceCode      @"DistrictSelectProvinceCode"
#define Key_DistrictSelectProvinceSubCode   @"DistrictSelectProvinceSubCode"
#define Key_DistrictSelectProvinceSub       @"DistrictSelectProvinceSub"
#define Key_DistrictSelectCityCode          @"DistrictSelectCityCode"
#define Key_DistrictSelectCity              @"DistrictSelectCity"
#import "QJMineEditAddressVC.h"
#import "GPDateView.h"
@interface QJMineEditAddressVC () <UITextViewDelegate>
//右侧功能按钮
@property (nonatomic)UIBarButtonItem* rightBarItem;
@end

@implementation QJMineEditAddressVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self basicUIPrepareAction];
}
- (void)basicUIPrepareAction{
    
    self.detailAddressTF.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
    if (!self.model) {  // 新增地址
        self.title = @"新增地址";
        self.detailAddressLabel.hidden = YES;
        self.detailAddressTF.text = @"街道门牌地址";
        self.detailAddressTF.textColor = [UIColor lightGrayColor];
    }else{ // 编辑地址
        self.title = @"编辑地址";
        self.nameTF.text = [NSString stringWithoutNilFormart:self.model.link_name];
        self.phoneTF.text = [NSString stringWithoutNilFormart:self.model.tel];
        self.cityTF.text = [NSString stringWithFormat:@"%@-%@-%@",self.model.province,self.model.city,self.model.region];
        self.detailAddressLabel.hidden = YES;
        self.detailAddressTF.text = [NSString stringWithoutNilFormart:self.model.detail_address];
    }
    self.detailAddressTF.delegate = self;
    
    UIButton *rightButton = [[UIButton alloc] init];
    [rightButton setTitleColor:HexRGB(0x29b5ed) forState:UIControlStateNormal];
    [rightButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
//    [rightButton sizeToFit];
    rightButton.frame = CGRectMake(0, 0, 80, 30);
    [rightButton addTarget:self action:@selector(rightBackItemClicked) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitle:@"保存" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = _rightBarItem;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavAlpha:YES];
    //控件不被导航栏遮挡
    self.navigationController.navigationBar.translucent = NO;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self setNavAlpha:NO];
    self.navigationController.navigationBar.translucent = YES;
}
#pragma mark -- 区域选择方法
- (IBAction)selectBtnClickAction:(UIButton *)sender {
    
    [self.nameTF resignFirstResponder];
    [self.phoneTF resignFirstResponder];
    [self.detailAddressTF resignFirstResponder];
   
    GPDateView * dateView = [[GPDateView alloc] initWithFrame:CGRectMake(0, UISCREEN_HEIGHT-250, UISCREEN_WIDTH, 250) Data:nil];
    
    [dateView showPickerView];
    
    dateView.ActionDistrictViewSelectBlock = ^(NSString *desStr,NSDictionary *selectDistrictDict){
        
        
        NSString *provine = [selectDistrictDict objectForKey:Key_DistrictSelectProvince];
        NSString *city = [selectDistrictDict objectForKey:Key_DistrictSelectCity];
        NSString *district = [selectDistrictDict objectForKey:Key_DistrictSelectProvinceSub];
        self.cityTF.text = [NSString stringWithFormat:@"%@-%@-%@",provine,city,district];
    };
}

#pragma mark -- UITextViewDelegate
//- (void)textViewDidChange:(UITextView *)textView{
//    if (self.detailAddressTF.text.length > 0) {
////        self.detailAddressLabel.hidden = YES;
//    }else{
////        self.detailAddressLabel.hidden = NO;
//        self.detailAddressTF.text = @"街道门牌地址";
//        self.detailAddressTF.textColor = [UIColor grayColor];
//    }
//}
- (void)textViewDidEndEditing:(UITextView *)textView{
    if(self.detailAddressTF.text.length < 1){
        self.detailAddressTF.text = @"街道门牌地址";
        self.detailAddressTF.textColor = [UIColor lightGrayColor];
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([self.detailAddressTF.text isEqualToString:@"街道门牌地址"]){
        self.detailAddressTF.text = @"";
        textView.textColor = HexRGB(0x666666);
    }
}

#pragma mark -- 保存按钮方法
- (void)rightBackItemClicked{
    if (self.nameTF.text.length > 0 && self.phoneTF.text.length > 0 && self.cityTF.text.length > 0 && self.detailAddressTF.text.length > 0) {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:self.nameTF.text forKey:@"consignee"];
        [dict setObject:self.phoneTF.text forKey:@"mobile"];
        NSArray *arr = [self.cityTF.text componentsSeparatedByString:@"-"];
        [dict setObject:arr[0] forKey:@"province_show"];
        [dict setObject:arr[1] forKey:@"city_show"];
        [dict setObject:arr[2] forKey:@"region_show"];
        [dict setObject:self.detailAddressTF.text forKey:@"address"];
        

        if (!self.model) {  // 新增地址
            
            NSLog(@"添加地址 dict = %@", dict);


            [WWNetRequest fetchAddAddressWithParameters:dict success:^(id response) {
                NSLog(@"添加地址 response = %@", response);
                if (response) {
                    [MBPHUD showAlertWithTitle:@"地址信息提交成功!"];
                    KPopViewControllerAction;
                }
            } failure:^(NSDictionary *error) {
                NSLog(@"添加地址 error = %@", error);
            }];
            
        }else{ // 编辑地址

            [WWNetRequest fetchEditAddressWithParameters:dict success:^(id response) {
            
                if (response) {
                    [MBPHUD showAlertWithTitle:@"地址修改成功!"];
                    KPopViewControllerAction;
                }
            } failure:^(NSDictionary *error) {
                NSLog(@"编辑地址 error = %@", error);
            }];
        }

    }else{
        [MBPHUD showAlertWithTitle:@"地址信息不完整!"];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
